package com.tianruan.dc.modules.sys.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tianruan.dc.common.utils.IdGen;
import com.tianruan.dc.modules.sys.user.entity.db.ConsultationDO;
import com.tianruan.dc.modules.sys.user.entity.dto.ConsultationDTO;
import com.tianruan.dc.modules.sys.user.entity.dto.TutorSolutionDTO;
import com.tianruan.dc.modules.sys.user.mapper.ConsultationMapper;
import com.tianruan.dc.modules.sys.user.service.IConsultationService;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import com.tianruan.dc.modules.sys.user.utils.UserUtils;
import com.tianruan.dc.modules.userinfo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;



@Service
@Slf4j

public class ConsultationServiceImpl implements IConsultationService {

    @Autowired
    private ConsultationMapper consultationMapper;

    /**
     * 新增咨询
     * @param consultationDTO
     */
    @Override
    public void save(ConsultationDTO consultationDTO) {
        consultationDTO.setId(IdGen.uuid());
        consultationDTO.setStatus(0);
        consultationDTO.setUser_name(UserUtils.getUser().getUsername());
        consultationDTO.setCreate_by(UserUtils.getUser().getName());
        consultationDTO.setCreate_date(LocalDateTime.now());
        consultationDTO.setUpdate_by(UserUtils.getUser().getName());
        consultationDTO.setUpdate_date(LocalDateTime.now());
        consultationDTO.setDel_flag(String.valueOf(0));
        consultationMapper.insert(consultationDTO);
    }

    /**
     * 删除咨询
     * @param consultationDO
     */
    @Override
    public void delete(ConsultationDO consultationDO, String id) {
        consultationDO.setDelFlag(String.valueOf(1));
        consultationMapper.deleteConsultation(id);
        consultationMapper.deleteSolution(id);
    }

    /**
     * 根据id查询咨询
     * @param id
     * @return
     */
    @Override
    public ConsultationDTO getById(String id) {
        return consultationMapper.getById(id);
    }

    /**
     * 修改咨询
     * @param consultationDTO
     */
    @Override
    public void update(ConsultationDTO consultationDTO) {
        consultationDTO.setUpdate_by(UserUtils.getUser().getUsername());
        consultationDTO.setUpdate_date(LocalDateTime.now());
        consultationMapper.update(consultationDTO);
    }

    /**
     * 查询分页查询
     */
    @Override
    public PageBean<ConsultationDTO> pageConsultation(Integer page, Integer pageSize, String project_brief, String question,
                                                      String create_by, Date startTime_C, Date endTime_C)
    {
        PageBean<ConsultationDTO> pageBean = new PageBean<>();
        PageHelper.startPage(page,pageSize);
        List<ConsultationDTO> pageInfo = consultationMapper.page(page,pageSize,project_brief,question,create_by,startTime_C,endTime_C);
        Page<ConsultationDTO> page1 = (Page<ConsultationDTO>) pageInfo;
        pageBean.setTotal(page1.getTotal());
        pageBean.setItems(page1.getResult());
        return pageBean;
    }

    /**
     * 解答记录分页查询
     */
    @Override
    public PageBean<TutorSolutionDTO> pageSolution(Integer page, Integer pageSize, String project_brief, String create_by,
                                                   String createBy, Date startTime_C, Date endTime_C, Date startTime_S, Date endTime_S) {
        //创建pageBean对象
        PageBean<TutorSolutionDTO> pageBean = new PageBean<>();
        //开启分页查询  和 模糊查询
        PageHelper.startPage(page,pageSize);
        //mapper
        List<TutorSolutionDTO> pageInfo = consultationMapper.list(page,pageSize,project_brief,create_by,createBy,startTime_C,endTime_C,startTime_S,endTime_S);
        //得到记录数填充pagebean对象返回
        Page<TutorSolutionDTO> page1 = (Page<TutorSolutionDTO>) pageInfo;
        pageBean.setTotal(page1.getTotal());
        pageBean.setItems(page1.getResult());
        return pageBean;
    }


     /**
     * 根据id查询导师
     * @param id
     * @return
     */
    @Override
    public User getTutorById(String id) {
        return consultationMapper.getTutorById(id);
    }

    /**
     * 根据用户id查询个人咨询与解答信息
     * @param user_name
     * @return
     */
    @Override
    public List<ConsultationDTO> getByUserId(String user_name) {
        return consultationMapper.getByUserId(user_name);
    }


}
